<?php
namespace app\controller\api\v1\housing;

use app\Request;
use think\facade\Db;

class HousingCollection

{

	public function housing_collection_add (Request $request)
	{

		[$housing_collection_id]= $request->postMore ([
			['housing_collection_id',''],
		], true);
		$data['user_id']=$request->uid;
		$data['housing_collection_id']=$housing_collection_id;
		$data['ctime']=date('Y-m-d H:i:s');
		$model = new \app\model\v1\housing\HousingCollection();

		$housing_collection = $model->where ('housing_collection_id',21)->where ('user_id',$request->uid)->find ();

		if ($housing_collection){
			$model->where ('housing_collection_id',21)->where ('user_id',$request->uid)->delete ();
		} else{
			$count =$model->where ('user_id',$request->uid)->count ();
			if ($count>50){
				return  app('json')->fail('收藏房源最多50个');
			}
			$model->insert ($data);
		}
		return app('json')->status('success', '成功');

	}


	public function housing_collection_list (Request $request)
	{
		[$type,$page,$limit]=$request->postMore([
			['type', ''],
			['page', 1],
			['limit', 30]
		], true);
		$Model = new \app\model\v1\housing\HousingSource();
		$data = $Model->alias ('a')
			->with ('HousingHousingLabelAssociation')
			->join('housing_collection r','r.housing_collection_id = a.id')
			->where(function ($a) use ($type) {
				if (!empty($type)){
					$a -> where ('a.housing_type', $type);
				}
			})
			->where ('r.user_id',$request->uid())
			->page ($page,$limit)
			->order ('ctime','desc')
			->field ('a.id,a.housing_source_door_head_img,a.architecture_area,a.architecture_rent,a.housing_source_title,a.type,a.is_proprietor,a.status,a.shop_type,housing_type,address')
			->select();
		return app('json')->status('success', '成功',$data);

	}


}
